from handlers.base_handler import BaseHandler
from handler_helpers.list_handler_helper import ListHandlerHelper
from auths.decorator import authenticated
from blls.user_blls.user_bll import UserBLL
from models.user_models.system_user_model import SystemUsers
from utils.check_params_utlil import CheckParams
from utils.handler_util import HandlerUtil


class LoginHandler(BaseHandler):
    @authenticated
    async def post(self):
        res = UserBLL(request=self).login()
        return self.write(HandlerUtil.set_response(data=res.data, message=res.message, is_success=res.is_success))


class RefreshTokenHandler(BaseHandler):
    @authenticated
    async def post(self):
        res = UserBLL(request=self).refresh_token()
        return self.write(HandlerUtil.set_response(data=res.data, message=res.message, is_success=res.is_success))


class UserListHandler(BaseHandler):
    @authenticated
    async def post(self):
        return ListHandlerHelper(request=self, bll_cls=UserBLL, cls=SystemUsers).list_handler()


class UserEditHandler(BaseHandler):
    @authenticated
    async def post(self):
        check_r = CheckParams(params=self.request_data,
                              conditions=[["guid", True, ""],
                                          ["role", True, 0]
                                          ]).check()
        if not check_r.is_success:
            return self.write(HandlerUtil.set_failure_response(message=check_r.message))
        guid, role = check_r.data.get('guid'), check_r.data.get('role')
        res = UserBLL(request=self).edit_user_role(guid, role)
        return self.write(HandlerUtil.set_response(data=res.data, message=res.message,
                                                   is_success=res.is_success, code=res.code))
